import { Router } from 'express';
import { WorkController } from '../../admin/controllers/workController';
import { authenticate, authorize } from '../../middleware/auth';
import { uploadWorkAudio } from '../../middleware/upload';

/**
 * 艺人作品管理路由
 * 提供作品CRUD操作的API接口
 */
const router = Router();
const workController = new WorkController();

// 应用认证中间件
router.use(authenticate);

// 获取作品列表
router.get('/', 
  authorize(['work:read']), 
  workController.getWorks.bind(workController)
);

// 获取作品详情
router.get('/:id', 
  authorize(['work:read']), 
  workController.getWork.bind(workController)
);

// 创建作品
router.post('/', 
  authorize(['work:create']), 
  workController.createWork.bind(workController)
);

// 更新作品
router.put('/:id', 
  authorize(['work:update']), 
  workController.updateWork.bind(workController)
);

// 删除作品
router.delete('/:id', 
  authorize(['work:delete']), 
  workController.deleteWork.bind(workController)
);

// 批量删除作品
router.delete('/batch', 
  authorize(['work:delete']), 
  workController.batchDeleteWorks.bind(workController)
);

// 获取作品统计信息
router.get('/statistics/overview', 
  authorize(['work:read']), 
  workController.getWorkStatistics.bind(workController)
);

// 获取艺人的作品列表
router.get('/artist/:artistId', 
  authorize(['work:read']), 
  workController.getArtistWorks.bind(workController)
);

/**
 * @swagger
 * /api/admin/works/{id}/audio:
 *   post:
 *     summary: 上传作品音频
 *     description: 为指定作品上传音频文件
 *     tags: [Works]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 作品ID
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               workAudio:
 *                 type: string
 *                 format: binary
 *                 description: 音频文件 (支持 mp3, wav, aac 等格式)
 *     responses:
 *       200:
 *         description: 上传成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 上传作品音频成功
 *                 data:
 *                   $ref: '#/components/schemas/ArtistWork'
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 作品不存在
 *       500:
 *         description: 服务器内部错误
 */
// 上传作品音频
router.post('/:id/audio', 
  authorize(['work:update']), 
  uploadWorkAudio,
  workController.uploadWorkAudio.bind(workController)
);

/**
 * @swagger
 * /api/admin/works/{id}/audio:
 *   delete:
 *     summary: 删除作品音频
 *     description: 删除指定作品的音频文件
 *     tags: [Works]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 作品ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 删除作品音频成功
 *                 data:
 *                   $ref: '#/components/schemas/ArtistWork'
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 作品不存在或没有音频文件
 *       500:
 *         description: 服务器内部错误
 */
// 删除作品音频
router.delete('/:id/audio', 
  authorize(['work:update']), 
  workController.deleteWorkAudio.bind(workController)
);

export default router;
